﻿using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
using Huirui.Cavan.Domain.MainBoundedContext.SystemModule;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spring.Context.Support;
using Huirui.Cavan.Domain.Seedwork;
using Huirui.Cavan.Core.Extension;

namespace Huirui.Cavan.Application.MainBoundedContext.SystemModule
{
    public class CorporationAppService : BaseAppService<Corporation, string, CorporationSearch, ICorporationRepository>, ICorporationAppService
    {
        private IIndustryTypeAppService IndustryAppService { get; set; }

        #region IAccountAppService Members

        public override BoolGenericResult<Corporation> AddOrUpdate(Corporation entity)
        {
            Init();
            BoolGenericResult<Corporation> result = new BoolGenericResult<Corporation>();

            Employee employee = this.EmployeeAppService.GetBySearch(SearchFactory.CreateEmployeeSearch(SecurityAppService.UserAccount.Id)).FirstOrDefault();
            Administrator admin = new Administrator(employee);

            Corporation oldCorporation = this.Repository.Get(entity.Id);
            if (oldCorporation.IsNull())
            {
                result = admin.AddCorporation(entity.Id, entity.Name, entity.Industry, entity.EnglishName, entity.MaxUser, entity.Contact, entity.Website, entity.Mobile, entity.Telephone, entity.Fax, entity.Email, entity.Area, entity.Address, entity.AddressEnglish, entity.DeliveryAddress2, entity.DeliveryAddress3, entity.DeliveryAddress1, entity.TaxIdentificationNumber, entity.Bank, entity.BankAccount);
                if (result.IsSuccess)
                    return base.Add(result.Object);
            }
            else
            {
                result = admin.UpdateCorporation(oldCorporation, entity.Name, entity.Industry, entity.EnglishName, entity.MaxUser, entity.Contact, entity.Website, entity.Mobile, entity.Telephone, entity.Fax, entity.Email, entity.Area, entity.Address, entity.AddressEnglish, entity.DeliveryAddress2, entity.DeliveryAddress3, entity.DeliveryAddress1, entity.TaxIdentificationNumber, entity.Bank, entity.BankAccount);
                if (result.IsSuccess)
                    return base.Update(result.Object);
            }
            return result;
        }
        #endregion
    }
}
